// const state = {}
// const mutaions = {}
// const actions = {}
// export default {
//   state,
//   mutaions,
//   actions
// }
import { setToken, getToken, removeToken } from '@/utils/auth'
import { getUserInfoAPI, loginAPI } from '@/api/user'
import { constantRoutes } from '@/router/index'
const state = {
  token: getToken(),
  userInfo: {},
  depmsData: [],
  routes: constantRoutes
}
const actions = {
  async login(context, payload) {
    const token = await loginAPI(payload)
    context.commit('setUserToken', token)
  },
  async getUserInfo(context) {
    const userInfo = await getUserInfoAPI()
    context.commit('setUserInfo', userInfo)
    return userInfo
  },
  logOut(context) {
    context.commit('removeUserToken')
    context.commit('setUserInfo', {})
  }
}
const mutations = {
  setUserToken(state, payload) {
    state.token = payload
    setToken(payload)
  },
  setUserInfo(state, payload) {
    state.userInfo = payload
  },
  removeUserToken(state) {
    removeToken()
    state.token = ''
  },
  setRoutes(state, newRoutes) {
    state.routes = [...constantRoutes, ...newRoutes]
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
